﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace ERP.Classes
{
    public class cs_hmr_usersInfo
    {
        private int _user_id;
        private int _id_nv;
        private int _groupid;
        private string _username;
        private string _pass;
        private DateTime _creatdate;
        private DateTime _lastlogin;
        private bool _status;
        private string _donvi_thaotac;

        
        public cs_hmr_usersInfo()
        {
            this._user_id = 0;
            this._id_nv = 0;
            this._groupid = 0;
            this._username = " ";
            this._pass = " ";
            this._creatdate = Convert.ToDateTime("01/01/9999");
            this._lastlogin = Convert.ToDateTime("01/01/9999");
            this._status = true;
            this._donvi_thaotac = " ";

        }
        public int user_id
        {
            get
            {
                return this._user_id;
            }
            set
            {
                this._user_id = value;
            }
        }
        public int id_nv
        {
            get
            {
                return this._id_nv;
            }
            set
            {
                this._id_nv = value;
            }
        }
        public int groupid
        {
            get
            {
                return this._groupid;
            }
            set
            {
                this._groupid = value;
            }
        }
        public string username
        {
            get
            {
                return this._username;
            }
            set
            {
                this._username = value;
            }
        }
        public string pass
        {
            get
            {
                return this._pass;
            }
            set
            {
                this._pass = value;
            }
        }
        public DateTime creatdate
        {
            get
            {
                return this._creatdate;
            }
            set
            {
                this._creatdate = value;
            }
        }
        public DateTime lastlogin
        {
            get
            {
                return this._lastlogin;
            }
            set
            {
                this._lastlogin = value;
            }
        }
        public bool status
        {
            get
            {
                return this._status;
            }
            set
            {
                this._status = value;
            }
        }
        public string donvi_thaotac
        {
            get
            {
                return this._donvi_thaotac;
            }
            set
            {
                this._donvi_thaotac = value;
            }
        }

        public int ExcuteStoreProcedure(string storeProcedure, int option)
        {
            string[] paraName = new string[10];
            paraName[0] = "@user_id";
            paraName[1] = "@id_nv";
            paraName[2] = "@groupid";
            paraName[3] = "@username";
            paraName[4] = "@pass";
            paraName[5] = "@creatdate";
            paraName[6] = "@lastlogin";
            paraName[7] = "@status";
            paraName[8] = "@donvi_thaotac";
            paraName[9] = "@option";
            object[] paraValue = new object[10];
            paraValue[0] = this._user_id;
            paraValue[1] = this._id_nv;
            paraValue[2] = this._groupid;
            paraValue[3] = this._username;
            paraValue[4] = this._pass;
            paraValue[5] = this._creatdate;
            paraValue[6] = this._lastlogin;
            paraValue[7] = this._status;
            paraValue[8] = this._donvi_thaotac;
            paraValue[9] = option;
            csCSDL csdl = new csCSDL();
            int i = 0;
            i = csdl.CreateParameters(paraName, 10, paraValue);
            return csdl.ExcuteStoreProcedure(storeProcedure);
        }
        public void KetDulieuVaoListview(ListView myList)
        {
            csListView list = new csListView();
            string[] fields = new string[9];
            fields[0] = "user_id";
            fields[1] = "id_nv";
            fields[2] = "groupid";
            fields[3] = "username";
            fields[4] = "pass";
            fields[5] = "creatdate";
            fields[6] = "lastlogin";
            fields[7] = "status";
            fields[8] = "donvi_thaotac";
            list.KetDulieu(myList, "HMR_Users", "", fields);
        }
        public int ExcuteProcedure(int option)
        {
            return this.ExcuteStoreProcedure("Proc_HMR_Users", option);
        }

        public void ShowDataInCombo(ComboBox cbo, string condition)
        {
            csComboBox cb = new csComboBox("id_nv", "user_id");
            cb.KetDuLieu(cbo, "HMR_Users", condition);
        }

        public int ShowDataGrid(DataGrid datagrid, string condition)
        {
            csDataGrid grid = new csDataGrid();

            string[] header = new string[9];
            header[0] = "user_id";
            header[1] = "id_nv";
            header[2] = "groupid";
            header[3] = "username";
            header[4] = "pass";
            header[5] = "creatdate";
            header[6] = "lastlogin";
            header[7] = "status";
            header[8] = "donvi_thaotac";

            string[] mappingname = new string[9];
            mappingname[0] = "user_id";
            mappingname[1] = "id_nv";
            mappingname[2] = "groupid";
            mappingname[3] = "username";
            mappingname[4] = "pass";
            mappingname[5] = "creatdate";
            mappingname[6] = "lastlogin";
            mappingname[7] = "status";
            mappingname[8] = "donvi_thaotac";

            int[] colwidth = new int[9];
            colwidth[0] = 12;
            colwidth[1] = 12;
            colwidth[2] = 12;
            colwidth[3] = 90;
            colwidth[4] = 1200;
            colwidth[5] = 24;
            colwidth[6] = 24;
            colwidth[7] = 3;
            colwidth[8] = 30;
            csCSDL csdl = new csCSDL();
            return grid.CreateTableStyle("HMR_Users", datagrid, csdl.OpenTable("HMR_Users", condition), header, mappingname, colwidth, 9);
        }
        public int Get_Info()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                ds = csdl.OpenTable("HMR_Users", "username = '" + this._username + "'");
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    return 0;
                }
                this._user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString().Trim());
                this._id_nv = Convert.ToInt32(ds.Tables[0].Rows[0]["id_nv"].ToString().Trim());
                this._groupid = Convert.ToInt32(ds.Tables[0].Rows[0]["groupid"].ToString().Trim());
                this._username = ds.Tables[0].Rows[0]["username"].ToString().Trim();
                this._pass = ds.Tables[0].Rows[0]["pass"].ToString().Trim();
                this._creatdate = Convert.ToDateTime(ds.Tables[0].Rows[0]["creatdate"].ToString().Trim());
                this._lastlogin = Convert.ToDateTime(ds.Tables[0].Rows[0]["lastlogin"].ToString().Trim());
                this._status = Convert.ToBoolean(ds.Tables[0].Rows[0]["status"].ToString().Trim());
                this._donvi_thaotac = ds.Tables[0].Rows[0]["donvi_thaotac"].ToString().Trim();
                return 1;
            }
            catch
            {
                return -1;
            }
        }
    }
}
